package DatagramSocket2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        // 1.创建DatagramSocket对象（客户端对象）
        DatagramSocket socket = new DatagramSocket();
        // 2.创建DatagramPacket对象封装需要发送的数据（数据包对象）
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("说话(输入exit退出)：");
            String msg = sc.nextLine();
            if(msg.equals("exit")){
                System.out.println("退出成功，欢迎下次光临^^");
                socket.close();
                break;
            }
            byte[] bytes = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length,
                    InetAddress.getLocalHost(),6666);
            // 3.使用DatagramSocket对象的send方法，传入DatagramPacket对象
            socket.send(packet);
        }

    }
}
